﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.App
{
    /// <summary>
    /// 负责构建WebApplication
    /// </summary>
    public class WebApplicationBuilder
    {
        public IWebHostEnvironment Environment { get; }

        public ConfigurationManager Configuration { get; }

        public IServiceCollection Services { get; }

        public ILoggingBuilder Logging { get; set; }

        public WebApplicationBuilder(
            ILoggingBuilder logging,
            IServiceCollection services,
            IWebHostEnvironment environment,
            ConfigurationManager configurationManager)
        {
            Logging = logging;
            Services = services;
            Environment = environment;
            Configuration = configurationManager;
        }

        public WebApplication Build()
        {
            var services = Services.BuildServiceProvider(new ServiceProviderOptions
            {
                ValidateOnBuild = true,
                ValidateScopes = true,
            });
            return new WebApplication(services);
        }
    }
}
